<?php

/**
 * @name 用户资金记录
 * @desc 控制器
 */
class AssetController extends UserCommonController
{
    /**
     * 构树链
     */
    public function coinAction()
    {
        $params = $this->post();
        $row = $params['pageSize'] ?? 12;
        $page = $params['page'] ?? 1;
        $opt = $params['opt']?? 1;

        $cond = ['user_id'=> $this->user_info['user_id']];
        if($opt == 1){
            //冻结
            $cond['cate#in'] = [1,5];
        }else if($opt == 2){
            //可用
            $cond['cate#in'] = [2,3,4,5,6,7,8,9,10];
        }

        $coinModel = CoinLogModel::getSingleton();
        $list = $coinModel->db
            ->field('id,cate,amount,f_amount,create_time')
            ->where($cond)
            ->order('id desc')
            ->limit(($page - 1) * $row, $row)
            ->select();
        $total = $coinModel->db
            ->where($cond)
            ->count();

        foreach($list as &$val){
            $val['description'] = CoinLogModel::$cateArr[$val['cate']];
            if($opt == 1){
                $val['status'] = CoinLogModel::$upOrdown[1][$val['cate']];
            }else if($opt == 2){
                $val['status'] = CoinLogModel::$upOrdown[2][$val['cate']];
            }
            if($val['amount']<=0){
                $val['amount'] = $val['f_amount'];
            }
        }

        $data = [
            'list' => $list,
            'total' => intval($total),
            'current'   => intval($page),
            'pageSize'  => intval($row),
            'page'  => intval($page),
            'pageNum'   => ceil($total/$row)
        ];

        $this->ajaxReturn(0, '', $data);
    }

    /**
     *积分列表
     */
    public function pointAction()
    {
        $params = $this->post();
        $row = $params['pageSize'] ?? 12;
        $page = $params['page'] ?? 1;
        $opt = $params['opt']?? 1;

        $cond = ['user_id'=> $this->user_info['user_id']];
        if($opt == 1){
            //金券
            $cond['cate#in'] = [1,5];
        }else if($opt == 2){
            //购物
            $cond['cate#in'] = [5,8];
        }

        $pointModel = PointLogModel::getSingleton();
        $list = $pointModel->db
            ->field('id,cate,amount,f_amount,create_time')
            ->where($cond)
            ->order('id desc')
            ->limit(($page - 1) * $row, $row)
            ->select();
        $total = $pointModel->db
            ->where($cond)
            ->count();

        foreach($list as &$val){
            $val['description'] = PointLogModel::$cateArr[$val['cate']];
            if($opt == 1){
                $val['status'] = PointLogModel::$upOrdown[1][$val['cate']];
            }else if($opt == 2){
                $val['status'] = PointLogModel::$upOrdown[2][$val['cate']];
            }
            if($val['amount']<=0){
                $val['amount'] = $val['f_amount'];
            }
        }

        $data = [
            'list' => $list,
            'total' => intval($total),
            'current'   => intval($page),
            'pageSize'  => intval($row),
            'page'  => intval($page),
            'pageNum'   => ceil($total/$row)
        ];

        $this->ajaxReturn(0, '', $data);
    }

    /**
     * 构树苗列表
     */
    public function saplingAction()
    {
        $params = $this->post();
        $row = $params['pageSize'] ?? 12;
        $page = $params['page'] ?? 1;

        $saplingModel = SaplingLogModel::getSingleton();
        $cond = ['user_id'=> $this->user_info['user_id']];
        $list = $saplingModel->db
            ->field('id,cate,amount,f_amount,create_time')
            ->where($cond)
            ->order('id desc')
            ->limit(($page - 1) * $row, $row)
            ->select();
        $total = $saplingModel->db
            ->where($cond)
            ->count();

        foreach($list as &$val){
            $val['description'] = SaplingLogModel::$cateArr[$val['cate']];
            $val['status'] = SaplingLogModel::$upOrdown[$val['cate']];
            if($val['amount']<=0){
                $val['amount'] = $val['f_amount'];
            }
        }

        $data = [
            'list' => $list,
            'total' => intval($total),
            'current'   => intval($page),
            'pageSize'  => intval($row),
            'page'  => intval($page),
            'pageNum'   => ceil($total/$row)
        ];

        $this->ajaxReturn(0, '', $data);
    }

}
